<?php

/**
 * Description of FaqController
 *
 * @author FFOZEU
 */
namespace Applications\Modules\Faq\Backend\Controller;

if( !defined('IN') ) die('Hacking Attempt');

use Library\BackController;
use Library\HttpRequest;
use Library\Classe\Form\Form;

class FaqController extends BackController{
    //put your code here
    
    public function executeListeFaq(HttpRequest $request){
        
        $this->page->addVar('title', 'FAQ');
        
        $manager = $this->managers->getManagerOf('Faq');
        
        $faq = $manager->getFaqListe();
        
        $this->page->addVar('faqlist', $faq);
        
        $this->leftcolumn();
        
    }
    
    private function leftcolumn(){
        $out = array();
        $out['faq.html']= 'Liste FAQ';
        $out['add-faq.html']= 'Ajouter une FAQ';
        
        return $this->page->addVar('left_content', $out);
        
    }
    
    public function executeCreate(HttpRequest $request){
        $this->page->addVar('title', 'ajouter une FAQ');
        $this->leftcolumn();
        $manager = $this->managers->getManagerOf('Faq');
        
        $faqForm = new Form('addFaq');
        $faqForm->action($request->requestURI());
        //ajout des champs
        $faqForm->add('Text', 'position')
                ->label('Position');
        
        $faqForm->add('Checkbox', 'isactive')
                ->label('Activé');
        
        $faqForm->add('Text', 'question')
                ->label('Question')
                ->required(true);
        
        $faqForm->add('Textarea', 'answer')
                ->label('Réponse')
                ->add_class('field_text');
        if($request->getExists('id')){
            $faqForm->add('Hidden', 'id')->value($request->getData('id'));
            $this->page->addVar('title', 'Modifier une FAQ');
            $faqForm->add('Submit', 'submit')->value('Modifier');
        }else{
            $faqForm->add('Submit', 'submit')->value('Ajouter');
        }
        
        if($request->getMethod('post')){
           //test de la validation du post
            if ($faqForm->is_valid($_POST)) {
                list($position, $isactive, $question, $answer) = $faqForm->get_cleaned_data('position','isactive','question','answer');
                $_POST['answer'] = htmlspecialchars($answer);
                $_POST['isactive'] = isset ($_POST['isactive'])?1:0;
                $_POST['position'] = intval($position);
                $_POST['question'] = strip_tags($question);
                if(!$request->getExists('id')){
                    if($manager->addFaq($_POST)){
                        $this->app()->httpResponse()->redirect('faq.html');
                    }else{
                        $this->errors = 'Echec lors de l\'enregistrement';
                    }
                }else{
                    if($manager->updateFaq($_POST)){
                        $this->app()->httpResponse()->redirect('faq.html');
                    }else{
                        $this->errors = 'Echec lors de la mise à jour';
                    }
                }
            }else{
                $faqForm->bound($_POST);
            }
        }else{
            if($request->getExists('id')){
               $faq = $manager->getFaqById(intval($request->getData('id')));
               $faq_array['isactive'] = $faq->getIsactive();
               $faq_array['position'] = $faq->getPosition();
               $faq_array['question'] = $faq->getQuestion();
               $faq_array['answer'] = html_entity_decode($faq->getAnswer());
               $faqForm->bound($faq_array);
            }else{
                $faqForm->bound($_POST);
            }
        }
        $this->page->addVar('errors', $this->errors);
        $this->page->addVar('faqForm', $faqForm);  
    }
    
    public function executeDelete(HttpRequest $request){
        
        $manager = $this->managers->getManagerOf('Faq');
        if($request->getExists('id')){
            $out['id'] = $request->getData('id');
            if($manager->deleteFaq($out)){
                $this->app()->httpResponse()->redirect('faq.html');
            }else{
                $this->app()->httpResponse()->redirect('faq.html');
            }
        }
    }
}

?>
